package com.ding.second.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ding.second.po.Product;
import com.ding.second.po.ProductImage;
import com.ding.second.po.User;
import com.ding.second.service.ProductImageService;
import com.ding.second.service.ProductServer;
import com.ding.second.service.UserService;

@Controller
@RequestMapping("/detail")
public class DetailAction {

	@Autowired
	private ProductServer productServer;
	@Autowired
	private UserService userServer;
	@Autowired
	private ProductImageService productImageServce;

	@RequestMapping("/info/{productId}")
	public String showDetail(Model model, @PathVariable String productId) {
		Product product = productServer.getProductById(productId);
		User user = userServer.getUserByUserId(product.getUserId() + "");
		List<ProductImage> productImages = productImageServce.gettImageByProductId(productId);

		user.setUserAvatar("avatar/" + user.getUserAvatar());
		
		for (int i = 0; i < productImages.size(); i++) {
			 productImages.get(i).setImageName("productImages/" + productImages.get(i).getImageName()); 
		}
		
		model.addAttribute(product);
		model.addAttribute(user);
		model.addAttribute("productImages", productImages);

		return "commodityDetail";
	}
}
